﻿using FreeSql;
using FYSJcrm.Core.IRepository.PermManage;
using FYSJcrm.Core.Model.PermManage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FYSJcrm.Core.Repository.PermManage
{
    public class ManagerRoleRepository : BaseRepository<ManagerRoleEntity>, IManagerRoleRepository
    {
        private IFreeSql _freeSql;

        public ManagerRoleRepository(IFreeSql fsql) : base(fsql, null, null) { _freeSql = fsql; }

        public async Task<bool> DeleteRoleAsync(int id)
        {
            using (var uow = _freeSql.CreateUnitOfWork())
            {
                var roleRepository = _freeSql.GetRepository<ManagerRoleEntity>();
                var permissionRepository = _freeSql.GetRepository<PermissionEntity>();

                roleRepository.UnitOfWork = uow;
                permissionRepository.UnitOfWork = uow;

                var flag = await roleRepository.DeleteAsync(p => p.Id == id);
                await permissionRepository.DeleteAsync(p => p.RoleId == id);

                uow.Commit();

                return flag > 0;
            }
        }

        public async Task<int> DeleteRoleAsync(int[] ids)
        {
            using (var uow = _freeSql.CreateUnitOfWork())
            {
                var roleRepository = _freeSql.GetRepository<ManagerRoleEntity>();
                var permissionRepository = _freeSql.GetRepository<PermissionEntity>();

                roleRepository.UnitOfWork = uow;
                permissionRepository.UnitOfWork = uow;

                var count = await roleRepository.DeleteAsync(p => ids.Contains(p.Id));
                await permissionRepository.DeleteAsync(p => ids.Contains(p.RoleId));

                uow.Commit();

                return count;
            }
        }

        public async Task<bool> InsertRoleAsync(ManagerRoleEntity managerRole, string[] permissions)
        {
            using (var uow = _freeSql.CreateUnitOfWork())
            {
                var roleRepository = _freeSql.GetRepository<ManagerRoleEntity>();
                var permissionRepository = _freeSql.GetRepository<PermissionEntity>();

                roleRepository.UnitOfWork = uow;
                permissionRepository.UnitOfWork = uow;

                managerRole = await roleRepository.InsertAsync(managerRole);

                if (permissions.Length > 0)
                {
                    List<PermissionEntity> list = new List<PermissionEntity>();

                    foreach (var item in permissions)
                    {
                        PermissionEntity permission = new PermissionEntity
                        {
                            RoleId = managerRole.Id,
                            Name = item
                        };

                        list.Add(permission);
                    }

                    await permissionRepository.InsertAsync(list);
                }

                uow.Commit();

                return managerRole.Id > 0;
            }
        }

        public async Task<bool> UpdateRoleAsync(ManagerRoleEntity managerRole, string[] permissions)
        {
            using (var uow = _freeSql.CreateUnitOfWork())
            {
                var roleRepository = _freeSql.GetRepository<ManagerRoleEntity>();
                var permissionRepository = _freeSql.GetRepository<PermissionEntity>();

                roleRepository.UnitOfWork = uow;
                permissionRepository.UnitOfWork = uow;

                await roleRepository.UpdateAsync(managerRole);
                var perms = await permissionRepository.Select.Where(p => p.RoleId == managerRole.Id).ToListAsync();
                List<PermissionEntity> list = new List<PermissionEntity>();

                if (permissions.Length > 0)
                {
                    foreach (var item in permissions)
                    {
                        PermissionEntity perm = perms.Where(p => p.Name == item).FirstOrDefault();
                        if (perm != null)
                        {
                            perms.Remove(perm);
                        }
                        else
                        {
                            perm = new PermissionEntity
                            {
                                RoleId = managerRole.Id,
                                Name = item
                            };
                            list.Add(perm);
                        }
                    }
                }

                if (perms.Count > 0) await permissionRepository.DeleteAsync(perms);
                if (list.Count > 0) await permissionRepository.InsertAsync(list);

                uow.Commit();

                return true;
            }
        }
    }
}
